//
//  HomePage_Vm.swift
//  Pods
//
//  Created by eme on 2017/3/24.
//
//

import RxSwift
import NetWorkCore
import ModelProtocol
import SwiftyJSON
import UtilCore

/*
 owner:cy
 update:2016年12月13日17:57:04
 info: 首页数据
 VC:   HomePage_Vc
 model: HomePageModel
 */
class HomePage_Vm :Base_Vm{
    /// 表示正在刷新中
    let loading = ActivityIndicator()
    /*
     接收返回的数据
     */
    /// 返回首页数据
    let homePageElements = Variable<[HomePageModel]>([])
    let indexElements = Variable<[IndexModel]>([])
    /*
     通用设置
     */
    /// 表示正在刷新中
    let refreshing = Variable(false)
    
    init(refreshTriger: Observable<Void>) {
        super.init()
//        refreshTriger.map{ HomePageApi.firstPage }
//            .emeRequestApiForArray(HomePageModel.self, activityIndicator: loading)
//            .subscribe(onNext: {[unowned self] (result) in
//                switch result {
//                case .success(let data):
//                    self.homePageElements.value = data
//                case .failure(let error):
//                    self.error.onNext(error)
//                }
//            }).disposed(by: disposeBag)
        
        refreshTriger
            .map{ HomePageApi.index }
            .emeRequestApiForArray(IndexModel.self, activityIndicator: loading)
            .subscribe(onNext: {[unowned self] (result) in
                switch result {
                case .success(let data):
                    self.indexElements.value = data.filter({ (item) -> Bool in
                        return item.filterFlag
                    })
                case .failure(let error):
                    self.error.onNext(error)
                }
            }).disposed(by: disposeBag)
        
    }
    
}

